package top.continew.minithreadpool.policy;

import top.continew.minithreadpool.concurrent.ThreadPoolExecutor;
import java.util.concurrent.BlockingQueue;

/**
 * 拒绝策略 - 丢弃最旧任务
 */
public class DiscardOldestPolicy implements RejectedExecutionHandler {
    @Override
    public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
        if (!executor.isShutdown()) {
            BlockingQueue<Runnable> queue = executor.getQueue();
            queue.poll(); // 丢弃队列中最老的任务
            executor.execute(r);
        }
    }
}
